package aspirecn.project3.config;

import aspirecn.project3.interceptor.LoginInterceptor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * packageName aspirecn.project3.config
 *
 * @author 邵一宸
 * @version JDK 17
 * @className WebMvcConfig
 * @date 2024/7/20
 * @description TODO
 */

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    // 实现静态资源的映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/")  // 映射swagger2
                .addResourceLocations("file:/workspaces/images/");  // 映射本地静态资源
    }



    @Bean
    public LoginInterceptor userTokenInterceptor() {
        return new LoginInterceptor();
    }

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // TODO 配置不需要权限拦截的路径
        registry.addInterceptor(userTokenInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/hello")
                .excludePathPatterns("/user/system/login")
//                .excludePathPatterns("/user/system/save")
                .excludePathPatterns("/user/system/logout")
                .excludePathPatterns("/goods/pass/*")
                .excludePathPatterns("/user/system/random");


        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
